package com.party.mobile.web.controller.photo;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.core.exception.BusinessException;
import com.party.core.model.photo.PhotoProject;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.photo.PhotoBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import com.party.mobile.web.dto.photo.output.ProjectListOutput;
import com.party.mobile.web.dto.photo.output.TagOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 相册项目
 * @Author: yangshoukun
 * @Date: 2018/6/5 9:56
 */
@Controller
@RequestMapping(value = "/photos/project")
public class PhotoController {

    @Autowired
    private PhotoBizService photoBizService;

    @Autowired
    private CurrentUserBizService currentUserBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 相册项目列表
     *
     * @param memberId
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public AjaxResult list(String memberId, Page page) {
        try {
            List<PhotoProject> photoProjects = photoBizService.list(memberId, page);
            return AjaxResult.success(photoProjects, page);
        } catch (BusinessException be) {
            return AjaxResult.error(AjaxResult.ERROR_CODE, be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail")
    public AjaxResult detail(String id) {
        try {
            PhotoProject photoProject = photoBizService.detail(id);
            return AjaxResult.success(photoProject);
        } catch (BusinessException be) {
            return AjaxResult.error(AjaxResult.ERROR_CODE, be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 分类列表
     *
     * @param photosId
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tags")
    public AjaxResult tags(String photosId, Integer type) {
        try {
            List<TagOutput> outputs = photoBizService.tags(photosId, type);
            return AjaxResult.success(outputs);
        } catch (BusinessException be) {
            return AjaxResult.error(AjaxResult.ERROR_CODE, be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 机构的项目列表
     *
     * @param request request
     * @param page    分页
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("orgProjectList")
    public AjaxResult orgProjectList(HttpServletRequest request, Page page) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<ProjectListOutput> projectList = photoBizService.getOrgProjectList(page, currentUser.getId());
            return AjaxResult.success(projectList, page);
        } catch (Exception e) {
            logger.error("获取相册列表数据异常", e);
            return AjaxResult.error(100, "获取相册列表数据异常");
        }
    }

    /**
     * 获取我参加的众筹的机构的相册
     *
     * @param request 当前用户
     * @param page    分页
     * @return 相册数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping("getMyPhotoList")
    public AjaxResult getMyPhotoList(HttpServletRequest request, Page page) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<PhotoProject> projectList = photoBizService.getMyPhotoList(page, currentUser.getId());
            List<ProjectListOutput> projectListOutputs = photoBizService.transfromList(projectList);
            return AjaxResult.success(projectListOutputs, page);
        } catch (Exception e) {
            logger.error("获取相册列表数据异常", e);
            return AjaxResult.error(100, "获取相册列表数据异常");
        }
    }

    /**
     * 获取我参加的众筹的机构的相册(小程序接口)
     *
     * @param request 当前用户
     * @param page    分页
     * @return 相册数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping("getXcxMyPhotoList")
    public AjaxResult getXcxMyPhotoList(HttpServletRequest request, Page page) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<PhotoProject> projectList = photoBizService.getMyPhotoList(page, currentUser.getId());
            return AjaxResult.success(projectList, page);
        } catch (Exception e) {
            logger.error("获取相册列表数据异常", e);
            return AjaxResult.error(100, "获取相册列表数据异常");
        }
    }
}
